package DFA;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import nfa_generator.NFANode;

public class DFAStateFactory {
	private static Map<Set<NFANode>, DFAStateInterface> nfaNodesToDFAState;
	private static int idCnt;
	
	static {
		nfaNodesToDFAState = new HashMap<Set<NFANode>, DFAStateInterface>();
		idCnt = 0;
	}
	
	public static DFAStateInterface generate (Set<NFANode> nfaNodes) {
		if (nfaNodes == null)
			throw new IllegalArgumentException ("nfaNodes argument must not be null");
		
		Set<NFANode> nfaNodesKey = new HashSet<NFANode>();
		nfaNodesKey.addAll(nfaNodes);
		for (NFANode nfaNode : nfaNodes)
			nfaNodesKey.addAll( nfaNode.epsilonTraverse() );
				
		DFAStateInterface dfaStateValue;
		if (nfaNodesToDFAState.containsKey(nfaNodesKey))
			dfaStateValue =  nfaNodesToDFAState.get(nfaNodesKey);
		else {
			dfaStateValue = new DFAState (idCnt, nfaNodesKey);
			++idCnt;
			nfaNodesToDFAState.put(nfaNodesKey, dfaStateValue);
		}
		
		return dfaStateValue;	
	}
}
